#!/bin/bash

if [ "$UID" != "0" ]; then
    echo "权限不足..."
    exit 1
fi

CMAKEPATH=/usr/bin/cmake
if [ -e $CMAKEPATH ]; then
    echo "fine cmake in $CMAKEPATH"
else
    echo "can not find cmake in $CMAKEPATH"
    exit 0
fi

TMPDIR=/tmp/cmocka
if [ -e "$TMPDIR" ]; then
    rm -vrf $TMPDIR
fi

mkdir -pv $TMPDIR

# download and extra cmocka
echo "download and extra cmocka"
cd $TMPDIR
wget https://cmocka.org/files/1.1/cmocka-1.1.5.tar.xz
tar vxf cmocka-1.1.5.tar.xz

# build and install cmocka
cd cmocka-1.1.5
sed -i 's/cmake_minimum_required(VERSION 3.5.0)/cmake_minimum_required(VERSION 2.8.0)/g' CMakeLists.txt
mkdir build
cd build
cmake ..
echo "build cmocka ... "
make
echo "install cmocka ..."
sudo make install
echo "ldconfig ..."
sudo ldconfig

echo "finish! enjoy..."